package com.jiu345.api.controller;

import com.jiu345.utils.AliOssUtil;
import org.dromara.common.core.domain.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.xnio.Result;

import java.util.UUID;

/**
 * package:FileUploadController
 * Date: 2024.03.22 17:07
 * author:Mr Lei
 * desc:文件上传
 */
@RestController

public class FileUploadController {
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file) throws Exception {
        //获取文件
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，从而防止文件被覆盖
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //存储到本地
//        file.transferTo(new File("C:\\Users\\Administrator\\Desktop\\image\\"+filename));
        //存储到阿里云
        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return R.ok(url);
    }
}
